/**
 * class  Component
 * 组件基类
 * author haicent
 * email haicent@126.com
 * version 0.01
 */
package as3.component
{
	import flash.events.Event;
	import flash.utils.Dictionary;
	
	import as3.component.core.free_internal;
	import as3.component.iface.IPopUp;
	import as3.component.iface.IRender;
	import as3.component.manager.UIManager;
	import as3.component.support.BaseSkin;
	
	use namespace free_internal

	public class Component extends FreeSprite implements IRender, IPopUp
	{		
		protected var _propertyChanged:Boolean = false;
		protected var _uiLibrary:Dictionary;
		protected var _defaultSkin:BaseSkin;
		protected var _tollTipsInfo:*;
		
		public function Component(masked:Boolean=false)
		{
			super(masked);
			_uiLibrary = new Dictionary();
			initUI();
			initCom();
			creatUI();
			initListener();
		}
		
		/**
		 *设置UI信息 
		 * 
		 */
		protected function initUI():void
		{
			 //持有当前使用皮肤引用
			_defaultSkin = UIManager.defaultSkin;
		}
		
		/**
		 *根据UI信息为组件创建皮肤 
		 * 
		 */		
		protected function creatUI():void
		{			
			for (var key:String in _uiLibrary)
			{
				var cls:Class = UIManager.defaultSkin.getUIClassByName(_uiLibrary[key]);
				if (cls)
				{
					this[key] = new cls();
				}
			}
		}
		
		/**
		 *初始化组件信息 
		 * 
		 */		
		protected function initCom():void
		{
			
		}
		
		/**
		 *初始化侦听事件 
		 * 
		 */		
		protected function initListener():void
		{
			//侦听组件被添加至舞台事件
			addEventListener(Event.ADDED_TO_STAGE, $addedToStageHandler, false, 0, true);
			//侦听组件被移除舞台事件
			addEventListener(Event.REMOVED_FROM_STAGE, $removedFromStageHandler, false, 0, true);
		}
		
		private function $addedToStageHandler(evt:Event):void
		{
			stage.addEventListener(Event.RENDER, $randerHandler);
			//如果组件属性有变化，则通知在下一帧重绘
			if (_propertyChanged) stage.invalidate();
		}
		
		private function $removedFromStageHandler(evt:Event):void
		{
			stage.removeEventListener(Event.RENDER, $randerHandler);
		}
		
		private function $randerHandler(evt:Event):void
		{
			//如果组件属性有修改，则执行重绘操作
			if (propertyChanged)
			{
				updateDisplay();
				propertyChanged = false;
			}
		}
		/**
		 * 移动组件 
		 * @param xpos X坐标
		 * @param ypos Y坐标
		 * 
		 */		 
		public function move(xpos:Number, ypos:Number):void
		{
			x = Math.round(xpos);
			y = Math.round(ypos);
		}
		
		public function setSize(w:Number, h:Number):void
		{
			width = w;
			height = h;
		}
		
		protected function set propertyChanged(value:Boolean):void
		{
			_propertyChanged = value;
			if (_propertyChanged && stage)
			{
				stage.invalidate();
			}
		}
		
		protected function get propertyChanged():Boolean
		{
			return _propertyChanged;
		}
		
		/**
		 *获得当前UI库 
		 * @return 
		 * 
		 */		
		protected function get uiLibrary():Dictionary
		{
			return _uiLibrary;
		}
		
		/**
		 *实现 IRender接口
		 * 
		 */		
		public function updateDisplay():void
		{
			if (!propertyChanged) return;
		}
		
		public function onPopUpOpen():void
		{
			
		}
		
		public function onPopUpClose():void
		{
			
		}
		
		public function set toolTips(value:*):void
		{
			if (value == null) 
			{
				if (_tollTipsInfo) 
				{
					//ToolTipsManager.getInstance().unRegister(this);
				}
				_tollTipsInfo = "";
			} 
			else 
			{
				if (_tollTipsInfo == value) 
				{
					return;
				}
				_tollTipsInfo = value;
				//ToolTipsManager.getInstance().register(this, _tollTipsInfo);
			}
		}
		
		public function get toolTips():*
		{
			return _tollTipsInfo;
		}
		
	}
}